﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Web;


namespace Small.Util.Common
{
   public class SysFilter
    {
        //// {D8D56CCE-2B91-4F73-9A8F-CC073620847F}
        //DEFINE_GUID(<<名称>>, 
        //0xd8d56cce, 0x2b91, 0x4f73, 0x9a, 0x8f, 0xcc, 0x7, 0x36, 0x20, 0x84, 0x7f);

       string[] spliceValues = new string[] { "#0x4#f73#" };
       string[] spliceValues1 = new string[] { "#0x2#b91#" };
       string allPredicate = "0xd8d56cce";
       private static SysFilter filter;
       private SysFilter() 
       {
       
       }
       public static SysFilter CreateInstance() 
       {
           if (filter == null) 
           {
               filter= new SysFilter();
           }
           return filter;
       }

       /// <summary>
       /// 获取到查询条件
       /// </summary>
       /// <param name="context"></param>
       /// <returns></returns>
       public FilterItem GetFilter(HttpContextBase context)
       {
           var predicate = string.Empty;
           var value=string.Empty;
           if (context.Request.QueryString["Predicate"] != null)
           {
               predicate = context.Request.QueryString["Predicate"];
               value=context.Request.QueryString["PredicateValue"];
           }
           else 
           {
               predicate = context.Request.Form["Predicate"];
               value = context.Request.Form["PredicateValue"];
           }
           FilterItem item = new FilterItem();
           item.Predicate = predicate;

           if (item.Predicate != allPredicate)
           {
               if (!String.IsNullOrEmpty(value))
               {
                   item.Values = ParseValue(context, value);
               }
           }
           else
           {
               item.Predicate = "1==1";
           }

           return item;
       }
       /// <summary>
       /// 获取到查询条件
       /// </summary>
       /// <param name="context"></param>
       /// <returns></returns>
       public  FilterItem GetFilter(HttpContext context)
       {
           var predicate = context.Request["Predicate"];
           var value = context.Request["PredicateValue"];
           FilterItem item = new FilterItem();
           item.Predicate = predicate;
           item.Values = ParseValue(context, value);
           return item;
       }
       /// <summary>
       /// 转换值
       /// </summary>
       /// <param name="context"></param>
       /// <param name="value"></param>
       /// <returns></returns>
       private  object[] ParseValue(HttpContext context, string value) 
       {
           List<object> objs = new List<object>();
           if (string.IsNullOrEmpty(value)) { return null; }
           string[] values = value.Split(spliceValues, StringSplitOptions.RemoveEmptyEntries);
           foreach (var item in values)
           {
               var unencodeItem = context.Server.HtmlDecode(item);
               objs.Add(ConvertItem(item));
           }
           return objs.ToArray();
       }

       private object[] ParseValue(HttpContextBase context, string value)
       {
         
           List<object> objs = new List<object>();
           if (string.IsNullOrEmpty(value)) { return null; }

           string[] values = value.Split(spliceValues, StringSplitOptions.RemoveEmptyEntries);
           foreach (var item in values)
           {
               var unencodeItem = context.Server.UrlDecode(item);
               objs.Add(ConvertItem( item));
           }

           return objs.ToArray();
       }
       private  object ConvertItem(string item) 
       {


           if (item == null) { throw new ArgumentNullException("value"); }
           var item1 = item.TrimStart('[').TrimEnd(']');
           var item2 = item1.Split(spliceValues1, StringSplitOptions.None);
           if (item2 == null) { return null; }
           switch (item2[0].ToLower())
           {
               case "s":
                   return Convert.ToString(item2[1].Trim());
               case "i":
                   int value=0;
                   Int32.TryParse(item2[1].Trim(), out value);
                   return value;
               case "n":
                   decimal value1=0;
                   Decimal.TryParse(item2[1].Trim(), out value1);
                    return value1;
               case "d":
                   double value2=0;
                   Double.TryParse(item2[1].Trim(), out value2);
                   return value2;
               case "l":
                   long value3 = 0;
                   Int64.TryParse(item2[1].Trim(), out value3);
                   return value3;

               case "dt":
                   DateTime value4 = DateTime.Now;
                   DateTime.TryParse(item2[1].Trim(), out value4);
                   return value4;
               default:
                   return Convert.ToString(item2[1].Trim());
                  
           }
       }
    }
}
